<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
</body>
<script>
    class Pet {
    #age;
    #nickname;

    constructor(nickname, age) {
        this.#nickname = nickname;
        this.setAge(age);
    }

    getAge() {
        return this.#age;
    }

    setAge(age) {
        if (age >= 0 && age <= 30) {
            this.#age = age;
        } else {
            throw new Error("年龄不合法");
        }
    }

    getNickname() {
        return this.#nickname;
    }
}

class Dog extends Pet {
    #color;

    constructor(nickname, age, color) {
        super(nickname, age);
        this.#color = color;
    }

    getColor() {
        return this.#color;
    }
}

let dog = new Dog('旺财', 3, '黄色');
console.log("宠物昵称：", dog.getNickname());
console.log("宠物年龄：", dog.getAge());
console.log("宠物颜色：", dog.getColor());
// 修改宠物年龄为40岁
dog.setAge(40);
</script>
</html>